# JustifiedTextMenuItem - Menuitem that contains a justified text label
# Copyright (C) 2007 Lauri Taimila
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

__licence__ = "GPLv2"
__copyright__ = "2007, Lauri Taimila"
__author__ = "Lauri Taimila <lauri@taimila.com>"

import pango
import clutter

from frontend.gui.widgets.menu_item import MenuItem

class JustifiedTextMenuItem(MenuItem):
    """
    Simple menuitem widget that contains text label. Label can be justified to
    left, right and center. Text is zoomed when menuitem is active
    """
    
    # Justification
    LEFT = 0
    RIGHT = 1
    CENTER = 2
    
    # Padding in pixels
    PADDING = 20
    
    def __init__(self, width, height, theme, text, justify=0):
        """
        Initialize menuitem widget.
        
        @param width: Widget width in pixels
        @param height: Widget height in pixels
        @param theme: Theme object
        @param label_text: Label text as a string
        """
        MenuItem.__init__(self, width, height)
        self.justify = justify
        self.theme = theme
        self.active_text_color = theme.getColor("menuitem_active")
        self.inactive_text_color = theme.getColor("menuitem_inactive")
        
        label = clutter.Label()
        label.set_name("text_label")
        label.set_text(text)
        label.set_color(self.inactive_text_color)
        label.set_font_name(theme.getFont() + " 28px")
        label.set_line_wrap(False)
        
        self.add(label)
        self._update_label()
        label.show()
                
    def set_active(self, boolean):
        """
        Set menuitem active or inactive. Changes the text color of the menuitem.
        @param boolean: use True to set active and False to set inactive
        """
        MenuItem.set_active(self, boolean)
        if boolean:
            for index in range(self.get_n_children()):
                actor = self.get_nth_child(index)
                if actor.get_name() == "text_label":
                    actor.set_color(self.active_text_color)
                    #actor.set_anchor_point_from_gravity(clutter.GRAVITY_CENTER)
                    actor.set_font_name(self.theme.getFont() + " 38px")
                    #actor.set_anchor_point(0,0)
                    self._update_label()
        else:
            for index in range(self.get_n_children()):
                actor = self.get_nth_child(index)
                if actor.get_name() == "text_label":
                    actor.set_color(self.inactive_text_color)
                    #actor.set_anchor_point_from_gravity(clutter.GRAVITY_CENTER)
                    actor.set_font_name(self.theme.getFont() + " 28px")
                    #actor.set_anchor_point(0,0)
                    self._update_label()

    def _update_label(self):
        """
        Calculate new label position and update it.
        """
        for index in range(self.get_n_children()):
            actor = self.get_nth_child(index)
            if actor.get_name() == "text_label":
                actor.set_width(self.get_width() - (self.PADDING * 2 ))
                y = int((self.get_height() - actor.get_height()) / 2)
                if self.justify == self.LEFT:
                    x = self.PADDING
                elif self.justify == self.RIGHT:
                    x = self.get_width() - self.PADDING
                elif self.justify == self.CENTER:
                    x = int((self.get_width() - actor.get_width()) / 2)
                actor.set_position(x,y)
                actor.set_ellipsize(pango.ELLIPSIZE_END)